package com.cbgolf.oa.model;

import android.support.v4.util.ArrayMap;
import android.util.Log;
import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.contract.IListStatisticsContract.IListStatisticsModel;
import com.cbgolf.oa.entity.ListBean;
import com.cbgolf.oa.entity.StatisticsBean;
import com.cbgolf.oa.event.EventStatistics;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.TimeUnit;
import org.greenrobot.eventbus.EventBus;

/**
 * @author lq
 */
public class ListStatisticsModelImp extends BaseModel implements IListStatisticsModel {

  @Override
  public void requestData(int dataType, StatisticsBean data, int requestEvent) {
    if (null == data) {
      Util.show("sorry,data is null");
      return;
    }
    Map<String, String> map = new ArrayMap<>();
    String url = WebAPI.statistics_player_list_get;
    switch (dataType) {
      case DataManager.STATISTICS_PLAYER_ALL:
        url = WebAPI.statistics_player_list_get;
        addPlayerParams(map, data);
        break;
      case DataManager.STATISTICS_BUSINESS_ALL:
        addBussinessParams(map, data);
        url = WebAPI.statistics_payway_list_get;
        break;
      case DataManager.STATISTICS_BOOK_ALL:
        addBookParams(map, data);
        url = WebAPI.statistics_book_list_get;
        break;
      case DataManager.STATISTICS_BILL_ALL:
        url = WebAPI.statistics_bill_list_get;
        addBillParams(map, data);
        break;
      default:
        break;
    }
    if (Util.isNull(url)) {
      Util.show("sorry,url is null");
      return;
    }
    map.put("pageSize", String.valueOf(pageSize));
    map.put("currentPage", (requestEvent == LOADMORE ? String.valueOf(page++) : "0"));
    Web.getOK(url, map, new NetCallBack(response -> {
      EventBus.getDefault().post(new EventStatistics.Builder()
          .setTarget(getMsgTarget(dataType))
          .setListStatisticsData(getList(response, ListBean.class))
          .setRequestEvent(requestEvent)
          .setError(errorMsg, errorCode)
          .build());
    }));
  }

  @Override
  public void filterCaddieData(String level, StatisticsBean mData, int requestEvent) {
    if (null == mData || Util.listIsNull(mData.listCaddie)) {
      backCaddieResult(level, null, requestEvent);
      return;
    }
    List<ListBean> listResult = new ArrayList<>();
    ExecutorService service = new ThreadPoolExecutor(1, 1, 0L,
        TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024),
        r -> new Thread(r), new AbortPolicy());
    service.execute(() -> {
      ListBean bean;
      for (int i = 0; i < mData.listCaddie.size(); i++) {
        if (Util.isEquals(mData.listCaddie.get(i).levelName, level)) {
          bean = new ListBean.Builder()
              .setCaddieId(mData.listCaddie.get(i).caddieId)
              .setCaddieName(mData.listCaddie.get(i).caddieName)
              .setCaddieNo(mData.listCaddie.get(i).caddieNo)
              .setPhone(mData.listCaddie.get(i).phone)
              .setLevelName(mData.listCaddie.get(i).levelName)
              .setPlayTime(mData.listCaddie.get(i).playTime + "")
              .setPlayTimes(mData.listCaddie.get(i).playTimes)
              .setDay(mData.endTime + "")
              .setPostfix(mData.dateUnit)
              .build();
          listResult.add(bean);
        }
      }
      backCaddieResult(level, listResult, requestEvent);
    });
    service.shutdown();
  }

  private void backCaddieResult(String levelName, List<ListBean> list, int requestEvent) {
    EventBus.getDefault().post(new EventStatistics.Builder()
        .setTarget(getMsgTarget(DataManager.STATISTICS_CADDIE_ALL))
        .setListStatisticsData(list)
        .setRequestEvent(requestEvent)
        .setlevelName(levelName)
        .setError(ErrorUtil.NODATA, ErrorUtil.NODATA_CODE)
        .build());
  }

  private void addBillParams(Map<String, String> map, StatisticsBean data) {
    map.put("day", String.valueOf(data.endTime));
    map.put("profix", TextUtil.Text(data.dateUnit));
    map.put("signingId", TextUtil.Text(data.signingId));
  }

  private void addBookParams(Map<String, String> map, StatisticsBean data) {
    if (data.startTime > 0) {
      map.put("startTime", String.valueOf(data.startTime));
    }
    if (data.endTime > 0) {
      map.put("endTime", String.valueOf(data.endTime));
    }
    map.put("lineType", TextUtil.Text(data.lineType));
    map.put("reserveType", TextUtil.Text(data.reserveType));
  }

  private void addBussinessParams(Map<String, String> map, StatisticsBean data) {
    if (data.startTime > 0) {
      map.put("startTime", String.valueOf(data.startTime));
    }
    if (data.endTime > 0) {
      map.put("endTime", String.valueOf(data.endTime));
    }
    map.put("code", TextUtil.Text(data.code));
    map.put("payCode", TextUtil.Text(data.payCode));
    map.put("payIncome", TextUtil.Text(data.payIncome));
  }

  private void addPlayerParams(Map<String, String> map, StatisticsBean data) {
    if (data.startTime > 0) {
      map.put("startTime", String.valueOf(data.startTime));
    }
    if (data.endTime > 0) {
      map.put("endTime", String.valueOf(data.endTime));
    }
    map.put("id", TextUtil.Text(data.barQueryId));
    map.put("come", TextUtil.Text(data.isCome));

  }

  private int getMsgTarget(int dataType) {
    int target = 0;
    switch (dataType) {
      case DataManager.STATISTICS_PLAYER_ALL:
        target = DataManager.STATISTICS_PLAYER_LIST;
        break;
      case DataManager.STATISTICS_BUSINESS_ALL:
        target = DataManager.STATISTICS_BUSINESS_LIST;
        break;
      case DataManager.STATISTICS_BOOK_ALL:
        target = DataManager.STATISTICS_BOOK_LIST;
        break;
      case DataManager.STATISTICS_CADDIE_ALL:
        target = DataManager.STATISTICS_CADDIE_LIST;
        break;
      case DataManager.STATISTICS_BILL_ALL:
        target = DataManager.STATISTICS_BILL_LIST;
        break;
      default:
        break;
    }
    return target;
  }


}
